import { useSelector, useDispatch } from 'react-redux';
import { fetchComponents } from '../features/componentInfos/componentInfosSlice';

/**
 * 自定义hook，用于获取项目中所有React组件的信息
 * @returns {{components: Array, loading: boolean, error: Error|null}} 组件信息、加载状态和错误信息
 */
export function useReactComponentInfos() {
  const dispatch = useDispatch();
  const { components, loading, error } = useSelector(state => state.componentInfos);

  useEffect(() => {
    dispatch(fetchComponents());
  }, [dispatch]);

  return { components, loading, error };
}